package com.cmbchina.creditcard.application.service;

import com.cmbchina.creditcard.application.api.CreditCardManage;
import com.cmbchina.creditcard.application.api.dto.CreditCardDTO;
import com.cmbchina.creditcard.domain.CardTypeEnum;
import com.cmbchina.creditcard.domain.model.BusinessCard;
import com.cmbchina.creditcard.domain.model.CreditCard;
import com.cmbchina.creditcard.domain.model.GoldCard;
import com.cmbchina.creditcard.domain.support.CreditCardRepository;

import java.math.BigDecimal;
import java.util.List;

public class CreditCardManageImpl implements CreditCardManage {
    private CreditCardRepository creditCardRepository;

    @Override
    public String createCard(String type) {
        CardTypeEnum cardTypeEnum = CardTypeEnum.getEnumByType(type);
        switch (cardTypeEnum){
            case GOLDEN:
                GoldCard goldCard = new GoldCard(type);
                creditCardRepository.saveCreditCard(goldCard);
                return goldCard.getId();
            case BUSINESS:
                BusinessCard businessCard = new BusinessCard(type);
                creditCardRepository.saveCreditCard(businessCard);
                return businessCard.getId();
                default:
                    return null;
        }
    }

    @Override
    public Boolean consume(String id, BigDecimal amount) {
        CreditCard creditCard = creditCardRepository.getCreditCardById(id);
        Boolean consumeFlag = creditCard.consume(amount);
        if(consumeFlag){
            creditCardRepository.updateCreditCard(creditCard);
        }else{
            return false;
        }
        return true;
    }

    @Override
    public Boolean repay(String id, BigDecimal amount) {
        CreditCard creditCard = creditCardRepository.getCreditCardById(id);
        Boolean consumeFlag = creditCard.repay(amount);
        if(consumeFlag){
            creditCardRepository.updateCreditCard(creditCard);
        }else{
            return false;
        }
        return true;
    }

    @Override
    public List<CreditCardDTO> queryCreditCardList() {
        //return creditCardRepository.queryCreditCardList();
        return null;
    }
}
